package com.cqupt.qualityevaluation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cqupt.qualityevaluation.controller.DTO.LoginDTO;
import com.cqupt.qualityevaluation.dao.UserMapper;
import com.cqupt.qualityevaluation.dao.expertMapper;
import com.cqupt.qualityevaluation.dao.schoolMapper;
import com.cqupt.qualityevaluation.dao.schooluserMapper;
import com.cqupt.qualityevaluation.pojo.*;
import com.cqupt.qualityevaluation.utils.jwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private schooluserMapper schooluserMapper;
    @Autowired
    private schoolMapper schoolMapper;
    @Autowired
    private expertMapper expertMapper;
//    @PostMapping("/login")
//    public String login(@RequestBody User u){
//        User res=userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername,u.getUsername()).eq(User::getPassword,u.getPassword()));
//        if (res==null){
//             return "null";
//         }
//         return "success";
//
//    }
//    @PostMapping("/login")
//    public User login(@RequestBody User u){
//        User res=userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername,u.getUsername()).eq(User::getPassword,u.getPassword()));
//        if (res!=null){
//            //添加token
//            u.setToken(jwtUtil.createToken());
//            return u;
//        }
//        return null;
//    }
    @PostMapping("/3/login")
    public User login3(@RequestBody User u){
        System.out.println(u);
        User res=userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername,u.getUsername()).eq(User::getPassword,u.getPassword()));
        if (res!=null){
            //添加token
            u.setToken(jwtUtil.createToken());
            return u;
        }
        return null;
    }
    @PostMapping("/2/login")
    public expert login2(@RequestBody expert e){
        expert res=expertMapper.selectOne(Wrappers.<expert>lambdaQuery().eq(expert::getUsername,e.getUsername()).eq(expert::getPassword,e.getPassword()));
        if (res!=null){
            //添加token
            e.setToken(jwtUtil.createToken());
            return e;
        }
        return null;
    }
//    @PostMapping("/1/login")
//    public schooluser login1(@RequestBody schooluser s){
//        schooluser res=schooluserMapper.selectOne(Wrappers.<schooluser>lambdaQuery().eq(schooluser::getUsername,s.getUsername()).eq(schooluser::getPassword,s.getPassword()));
//        System.out.println(res);
//        if (res!=null){
//            //添加token
//            s.setToken(jwtUtil.createToken());
//            return s;
//        }
//        return null;
//    }
    @PostMapping("/1/login")
    public schooluser login1(@RequestBody schooluser s){
        schooluser res=schooluserMapper.selectOne(Wrappers.<schooluser>lambdaQuery().eq(schooluser::getUsername,s.getUsername()).eq(schooluser::getPassword,s.getPassword()));
        System.out.println(res);
        if (res!=null){
            //添加token
            s.setToken(jwtUtil.createToken());
            return s;
        }
        return null;
    }
    @GetMapping("/checkToken")
    public Boolean checkToken(HttpServletRequest request){
        String token = request.getHeader("token");
        return jwtUtil.checkToken(token);
    }
    @GetMapping("/findAll")
    public List<User> findAll(){
        return userMapper.selectList(null);
    }
//    @PostMapping("/save")
//    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
//    public String save(@RequestBody User u){
//        userMapper.insert(u);
//        return "success";
//    }
    @PostMapping("/save")
    //通过RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的),将JSON对象转换为java对象
    public Object save(@RequestBody User u){
       Map map=new HashMap<>();
       int result=0;
       try {
           result=userMapper.insert(u);
       }catch (org.springframework.dao.DuplicateKeyException e){
           map.put("code","fail");
           map.put("msg","该角色名已存在");
           return map;
       }
       if (result==1){
           map.put("code","success");
           map.put("msg","添加成功");
       }
       else {
           map.put("code", "fail");
           map.put("msg","添加失败");
       }
       return map;
    }
    @DeleteMapping("/delete/{username}")
    public String delete(@PathVariable("username") String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        userMapper.delete(queryWrapper);
        return "success";
    }
    @PutMapping("/update")
    public Object update(@RequestBody User u){
        Map map=new HashMap<>();
        int result=0;
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",u.getId());
        User user = new User();
        user.setUsername(u.getUsername());
        user.setPassword(u.getPassword());
        try {
            result=userMapper.update(user,updateWrapper);
        }catch (org.springframework.dao.DuplicateKeyException e){
            map.put("code","fail");
            map.put("msg","该角色名已存在");
            return map;
        }
        if (result==1){
            map.put("code","success");
            map.put("msg","更新成功");
        }
        else {
            map.put("code", "fail");
            map.put("msg","添加失败");
        }
        return map;
    }
//    @PutMapping("/update")
//    public int update(@RequestBody User u){
//        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("id",u.getId());
//        User user = new User();
//        user.setUsername(u.getUsername());
//        user.setPassword(u.getPassword());
//        return userMapper.update(user,updateWrapper);
//    }
    @GetMapping("/selectone/{id}")
    public User findByUserId(@PathVariable("id") String id){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return userMapper.selectOne(queryWrapper);
    }
    @GetMapping("/search/1/{username}")
    public List<User> findByUserUsername(@PathVariable("username") String username){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username",username);
        return userMapper.selectList(queryWrapper);
    }
    @GetMapping("/search/2/{password}")
    public List<User> findByUserPassword(@PathVariable("password") String password){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("password",password);
        return userMapper.selectList(queryWrapper);
    }
}

